package com.see.server.command;

import java.util.HashMap;

import com.see.domain.Protocol;
import com.see.exception.MessegeFeedException;
import com.see.exception.UserExistException;
import com.see.server.ServerCore;
import com.see.server.SocketConnection;
import com.see.servise.SEEServise;

public class LoginCommand implements Command {

	private String username;
	private SocketConnection socketConnection;
	
	public LoginCommand(SocketConnection socketConnection ,HashMap<String, String> paramMap){
		username = paramMap.get("USERNAME");
		this.socketConnection = socketConnection;
	}
	
	@Override
	public String execute() throws MessegeFeedException, UserExistException {
		
		if (username == null || username.equals("")) {
			throw new MessegeFeedException(Protocol.errormsg(Protocol.LOGIN_FIELD, Protocol.ERROR_USERNAME));
		}
		
		try {
			ServerCore.getInstance().addConnection(username, socketConnection);
		} catch (UserExistException e) {
			throw new UserExistException(Protocol.errormsg(Protocol.LOGIN_FIELD, Protocol.ERROR_USEREXIST));
		}
		
		return Protocol.successmsg(Protocol.LOGIN_FIELD, "");
	}
	
}
